/**   
 * @Title: RechargeCaifutong.java 
 * @Package com.esun.elk.ui.views.userinfo.recharge 
 * @Description: 财付通充值
 * @author Kuroky Chengxt@500wan.com
 * @date 2011-11-01 上午11:11:11  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.recharg;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.config.Language;
import com.esun.rabbit2.communicaters.recharg.CaifutongTokenIDCommunicater;
import com.esun.rabbit2.viewer.WebViewActivity;
import com.esun.ui.R;
import com.esun.util.ViewerUtil;
import com.esun.util.log.EDebug;

/**
 * @ClassName: RechargeCaifutong
 * @Description: (财付通充值界面)
 * @author: Kuroky Chengxt@500wan.com
 * @date: 2012-02-07 下午2:06:12
 * 
 */
public class RechargeCaifutong extends Activity implements android.view.View.OnClickListener {

	// 提交按钮
	private Button btnSubmit;
	// 返回按钮
	private ImageButton btnBack;
	// 金额
	private EditText etMoney;

	private String money = Config.NULLMARK;
	private String tokenId = Config.NULLMARK;
	
	// 财付通充值通讯类
	private CaifutongTokenIDCommunicater orderIDCommunicater;
	private Handler rechargeHandler;
	private ProgressDialog progressOrder;

	private static final int GET_TOKENID_SUCCEED = 0x0000;
	private static final int GET_TOKENID_FAILED = 0x0001;
	
	// 财付通初始化成功返回id
	private static final String TOKEN_ID = Key.KEY_TOKEN_ID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.recharge_cft_act);

		init();
		initHandler() ;
	}

	// 初始化
	private void init() {

		progressOrder = ViewerUtil.showProgressDialog(RechargeCaifutong.this, Language.TIP_TRANSACTION_INIT);
		orderIDCommunicater = new CaifutongTokenIDCommunicater();

		btnSubmit = (Button) findViewById(R.id.recharge_cft_btn_submit);
		btnSubmit.setOnClickListener(this);

		btnBack = (ImageButton) findViewById(R.id.recharge_cft_btn_goBack);
		btnBack.setOnClickListener(this);

		etMoney = (EditText) findViewById(R.id.recharge_cft_edt_num);

	}

	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {

		case R.id.recharge_cft_btn_goBack:
			// 返回处理
			RechargeCaifutong.this.finish();

			break;

		case R.id.recharge_cft_btn_submit:

			// 获取充值金额，并且进行财付通初始化
			EDebug.o(this, "点击提交");
			if (etMoney.getText() != null) {
				money = etMoney.getText().toString();
			}

			if (money != null && !money.equals(Config.NULLMARK)) {
					int moneynum = Integer.parseInt(money);
					if(moneynum != 0){//填入金额不为零，最小为1
						progressOrder.show() ;
						// 财付通初始化
						getTokenId(money) ;
					}else{//填入金额为零
						ViewerUtil.showTipDialog(RechargeCaifutong.this, Language.TIP_INPUT_RIGHT_NUM, false);
					}
			} else {
				ViewerUtil.showTipDialog(RechargeCaifutong.this, Language.TIP_INPUT_RIGHT_NUM, false);
			}

			break;

		default:
			break ;
		}
	}
	
	// 财付通初始化，获取初始化id
	public void getTokenId(final String money) {
		
		new Thread() {

			@Override
			public void run() {
				super.run();

				try {
					
					// 获取版本号
					String version = RechargeCaifutong.this.getPackageManager().getPackageInfo(Config.APP_PACKAGE_INFO, 0).versionName ;
					// 初始化成功
					if (orderIDCommunicater.isGetOrderIDSucceed(version, money)) {
						Message msg = rechargeHandler.obtainMessage(GET_TOKENID_SUCCEED, orderIDCommunicater.getResult().get(TOKEN_ID)) ;
						rechargeHandler.sendMessage(msg) ;
					}else {
						// 初始化失败
						Message msg = rechargeHandler.obtainMessage(GET_TOKENID_FAILED, null);
						rechargeHandler.sendMessage(msg) ;
					}

				} catch (Exception e) {
					// 异常处理
					Message msg = rechargeHandler.obtainMessage(Config.CONNECTION_FAIL, null);
					rechargeHandler.sendMessage(msg);
				} 
			}

		}.start() ;
		
	}
	
	public void initHandler () {
		rechargeHandler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				
				switch (msg.what) {
				
				// 财付通初始化成功
				case GET_TOKENID_SUCCEED :
					
					progressOrder.dismiss() ;
					tokenId = (String)msg.obj ;
					
					// 跳转向财付通支付中心界面
					String url = Config.CAIFUTONG_REQUEST_URL + tokenId ;
					startActivity(new Intent(RechargeCaifutong.this,WebViewActivity.class).putExtra("title", Language.TIP_CFT_RECHARGE).putExtra("url", url));			
					
					break ;
				
				// 财付通初始化失败
				case GET_TOKENID_FAILED :
					
					progressOrder.dismiss() ;
					ViewerUtil.showTipDialog(RechargeCaifutong.this, Language.TIP_CFT_INIT_FAIL, false);
					break ;
					
				case Config.CONNECTION_FAIL:
					// 通讯异常提示
					progressOrder.dismiss();
					
					//ViewerUtil.showTipDialog(RechargeCaifutong.this, Language.STR_CONNECTIONFAIL, false);
					Toast.makeText(RechargeCaifutong.this, Language.STR_CONNECTIONFAIL.toString(), Toast.LENGTH_SHORT).show();
					break;
				
				default :
					break ;
				}

			}
		} ;
	}
}
